/*******************
This uart.c file is for STC90C52 MCU 
controlling the fan of some buildings


Functions:
send and receive uart data.

*******************/
#include "STC89C51RC_RD_PLUS.H"
#include <intrins.h>

extern char button_flag;
extern unsigned char time_flag1;
extern unsigned char time_flag2;
extern unsigned char lcd_flag1;
extern unsigned char lcd_flag2;
extern unsigned char lcd_flag3;
//extern unsigned char lcd_mark;

extern void bl55077_init(void);
extern void lcd_clear(void);
extern void lcd_allon(void);
extern void lcd_set1(void);
extern void lcd_set2(void);
extern void lcd_set3(void);
extern void lcd_test1(void);
extern void lcd_test2(void);


unsigned char tmp123=0;

#define FOSC 110592000L
#define BAUD 9600

//Define Uart parity mode
#define NONE_PARITY 	0
#define ODD_PARITY 		1
#define EVEN_PARITY 	2
#define MARK_PARITY 	3
#define SPACE_PARITY 	4

#define PARITYBIT EVEN_PARITY

bit busy;


void SendData(char dat);
void SendString(char *s);
void UartInit(void);

void UartInit(void)
{
	SCON=0x50;//none parity, 8bit uart
	TMOD=0x20;//set timer1 as 8bit auto reload mode
	PCON |= 0x80 ; //SMOD=1; 
//	TH1=TL1=-(FOSC/12/32/BAUD);//set auto reload value
	TH1=0xf4;//Baud:4800 fosc=11.0592MHz 
	TR1=1;//timer1 start run
	ES=1;//enable uart interrupt
	EA=1;//open master interrupt switch

}

//uart interrupt serive routine
void Uart_Isr() interrupt 4 using 1
{
	if(RI)
	{
	RI=0;
	tmp123=SBUF;
	button_flag=0;
	if(tmp123==0x12)
		{
//		time_flag1=1;
		}
	if(tmp123==0x34)
		{
//		time_flag2=1;
		}
	if(tmp123==0x56)
		{
		lcd_flag1=1;
		}
	if(tmp123==0x78)
		{
		lcd_flag2=1;
		}
	if(tmp123==0xaa)
		{
		lcd_flag3=1;
		}
	

//	lcd_mark=tmp123;

	}

	if(TI)
	{
	TI=0;
	busy=0;
	
	}

}

//Send a byte data to uart
void SendData(char dat)
{
	while(busy);//wait for the completion of the previous data
	ACC=dat;
	busy=1;
	SBUF=ACC;


}

//Send a string to uart
void SendString(char *s)
{
	while(*s)	//check the end of the string
	{
	SendData(*s++);//send current char and increment string ptr
	}

}











